package com.handu.luna.core.domain.advertisement;


import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;

import com.handu.luna.core.domain.BasePersistentDomain;
import com.handu.luna.core.domain.search.AdPositionSearch;



@Entity
public class AdPositionId extends BasePersistentDomain<AdPositionIdRepository>{

	@NotNull(message="广告位名称长度不能为空")
	@Size(min=1,max=200, message="广告位名称长度不能为空或超过200个字。")
	private String adPositionName;  //广告位名称	
	private String adPositionSymbol;  //广告位唯一标示符	  值唯一，见名识义
	//可选类型，可选值为 首页，分类页，搜索页 ， 详情页， 。
	private BelongPage belongPage;  //所属页面 ，例如：index_banaer   要定义为枚举类型的
	
	public enum BelongPage{ 
		INDEXPAGE("首页"),CATEGORYPAGE("分类页"),SEARCHPAGE("搜索页"),DETAILSPAGE("详情页"),TOPICPAGE("专题页");		
	    public String description;	    
	    BelongPage(final String description) {
	        this.description = description;
	    }
	}
	
	
    private int adPositionWidth;   //位置宽度
    private int adPositionHeight;   //、位置高度
	private String  Description;   //广告位描述
	
	@Transient
	private List<AdvertiseList> advertiseList;
	
	
	public String getAdPositionName() {
		return adPositionName;
	}

	public void setAdPositionName(String adPositionName) {
		this.adPositionName = adPositionName;
	}

	public String getAdPositionSymbol() {
		return adPositionSymbol;
	}

	public void setAdPositionSymbol(String adPositionSymbol) {
		this.adPositionSymbol = adPositionSymbol;
	}

	public BelongPage getBelongPage() {
		return belongPage;
	}

	public void setBelongPage(BelongPage belongPage) {
		this.belongPage = belongPage;
	}

	public int getAdPositionWidth() {
		return adPositionWidth;
	}

	public void setAdPositionWidth(int adPositionWidth) {
		this.adPositionWidth = adPositionWidth;
	}

	public int getAdPositionHeight() {
		return adPositionHeight;
	}

	public void setAdPositionHeight(int adPositionHeight) {
		this.adPositionHeight = adPositionHeight;
	}

	public String getDescription() {
		return Description;
	}

	public void setDescription(String description) {
		Description = description;
	}
	
	public List<AdvertiseList> getAdvertiseList() {
		return advertiseList;
	}

	public void setAdvertiseList(List<AdvertiseList> advertiseList) {
		this.advertiseList = advertiseList;
	}

	public AdPositionId(){  //构造函数
		
	}
	
	 public AdPositionId(String adPositionName , String adPositionSymbol,BelongPage belongPage,int adPositionWidth,int adPositionHeight,String  Description){
	    	super();
	    	this.adPositionName = adPositionName;
	    	this.adPositionSymbol = adPositionSymbol;
	    	this.belongPage = belongPage;
	    	this.adPositionWidth = adPositionWidth;
	    	this.adPositionHeight = adPositionHeight;
	    	this.Description = Description;
	}
	 
	 public Page<AdPositionId> findAll(Pageable pageable){
		 Page<AdPositionId> adPositionId = context.getAdPositionIdRepository().findAll(pageable);
		 return adPositionId;
	 }
	 public Page<AdPositionId> findAll(AdPositionSearch  adPositionSearch,Pageable pageable){
			Page<AdPositionId> adPositionId = context.getAdPositionIdRepository().findAll(AdPositionId.selectCondition(adPositionSearch), pageable);
			return adPositionId;
	 }
	 
	 //搜索
		public static Specification<AdPositionId> selectCondition(AdPositionSearch adPositionSearch) {
			return new Specification<AdPositionId>() {
				@Override
				public Predicate toPredicate(Root<AdPositionId> root,
						CriteriaQuery<?> query, CriteriaBuilder builder) {

					Predicate predicate = builder.conjunction();
					List<Expression<Boolean>> expressions = predicate.getExpressions();
					
				if(adPositionSearch!=null){
					if (StringUtils.isNotBlank(adPositionSearch.getAdPositionName())) {
						expressions.add(builder.like(root.<String> get("adPositionName"), "%"+adPositionSearch.getAdPositionName() + "%")); // 广告位名称
					}
					
					if (StringUtils.isNotBlank(adPositionSearch.getAdPositionBelongToPage())) {	
						switch (adPositionSearch.getAdPositionBelongToPage()) {
						case "INDEXPAGE":
							expressions.add(builder.equal(root.<AdPositionId.BelongPage> get("belongPage"), BelongPage.INDEXPAGE)); // 所属页面
					        break;
						case "CATEGORYPAGE":
							expressions.add(builder.equal(root.<AdPositionId.BelongPage> get("belongPage"), BelongPage.CATEGORYPAGE)); // 所属页面
					        break;
						case "SEARCHPAGE":
							expressions.add(builder.equal(root.<AdPositionId.BelongPage> get("belongPage"), BelongPage.SEARCHPAGE)); // 所属页面
					        break;
						case "DETAILSPAGE":
							expressions.add(builder.equal(root.<AdPositionId.BelongPage> get("belongPage"), BelongPage.DETAILSPAGE)); // 所属页面
					        break;
						case "TOPICPAGE":
							expressions.add(builder.equal(root.<AdPositionId.BelongPage> get("belongPage"), BelongPage.TOPICPAGE)); // 所属页面
					        break;
						}
					}
				}
					return query.where(predicate).getRestriction();
				
				}
			};
		}

	
}
